package com.meteor.downloader.videodownloader.admdm

import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.meteor.downloader.videodownloader.MdmDealApplication
import com.meteor.downloader.videodownloader.utils.DataManagerMdmUtils
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_CUR_NOTIFY_AD_SHOW

object BookIntersUtils {
    fun loadTIAdInfo(
        valueOriInfo: ValueMdmInfo, end: (Boolean) -> Unit
    ) {
        if (valueOriInfo.mdmAdID.isEmpty()) {
            end.invoke(false)
            return
        }
        if (!valueOriInfo.mBookLoading) {
            valueOriInfo.mBookLoading = true
            val adRequest = AdRequest.Builder().build()
            InterstitialAd.load(
                MdmDealApplication.mMdmContext,
                valueOriInfo.mdmAdID,
                adRequest,
                object : InterstitialAdLoadCallback() {
                    override fun onAdLoaded(interstitialAd: InterstitialAd) {
                        valueOriInfo.mIntersObject = interstitialAd
                        valueOriInfo.mLoadTime = System.currentTimeMillis()
                        valueOriInfo.mBookLoading = false
                        end.invoke(true)
                    }

                    override fun onAdFailedToLoad(p0: LoadAdError) {
                        valueOriInfo.mBookLoading = false
                        end.invoke(false)
                    }
                })
        } else {
            end.invoke(false)
        }
    }

    fun showMdmAdValue(
        activity: AppCompatActivity,
        valueOriInfo: ValueMdmInfo,
        mStatusLook: Boolean,
        listener: ShowPageAdListener
    ) {
        if (activity.isFinishing || activity.isDestroyed) return
        if (valueOriInfo.mBookShowing) {
            listener.dismissAFailed()
            return
        }
        if (!MdmDealApplication.mAppMdmOpen) {
            listener.dismissAFailed()
            return
        }
        valueOriInfo.mIntersObject?.let { mShowAd ->
            mShowAd.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdShowedFullScreenContent() {
                    mShowAd.setOnPaidEventListener {
                        AdInfoUtils.adValueUpload(activity, it)
                        AdInfoUtils.setAdSeMdmInters(it, mShowAd)
                    }
                    listener.successBack()
                    DataManagerMdmUtils.setDataKeyMdm(MDM_CUR_NOTIFY_AD_SHOW, 1)
                }

                override fun onAdDismissedFullScreenContent() {
                    listener.dismissAFailed()
                    valueOriInfo.changeResultToNull()
                }

                override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                    valueOriInfo.changeResultToNull()
                    listener.dismissAFailed()
                }
            }
            valueOriInfo.mBookShowing = true
            mShowAd.show(activity)
        } ?: run {
            listener.dismissAFailed()
        }
    }
}